package com.vip.filter;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginFilter extends HttpServlet implements Filter {

	private static final long serialVersionUID = 1L;
	@Override
	public void doFilter(ServletRequest req, ServletResponse res, FilterChain fc) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) res;
		response.setContentType("text/html;charset=UTF-8");
		HttpSession session = request.getSession();
		if (session.getAttribute("Admin") == null) {
			PrintWriter out = null;
			try {
				out = response.getWriter();
				out.println("<script language='javascript'>alert('应用的推送配置信息未登录或超时，请重新登录！');top.location.href='"+request.getContextPath()+"/logout';</script>");
			} catch (IOException ex1) {
				ex1.printStackTrace();
			} finally {
				out.close();
			}
		}else{
			fc.doFilter(req, res);
		}
	}

	@Override
	public void init(FilterConfig fc) throws ServletException {
		
	}
}
